import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:speed_random/pages/home/rand_list/logic.dart';
import 'state.dart';




class RandListListLogic extends GetxController {
  final RandListListState state = RandListListState();


  // 添加 TextEditingController
  final textController = TextEditingController();

// 添加项目到列表的方法
  void addItem(String item) {
    state.historyNumbers.value.add(item);
    state.historyNumbers.refresh();
  }
  void removeItem(int index) {
    state.historyNumbers.value.removeAt(index);
    state.historyNumbers.refresh();
  }
  @override
  void onClose() {
    textController.dispose();
    super.onClose();
  }


  @override
  void onInit() {
    super.onInit();
    initCache();
  }
  // ... 其他代码保持不变 ...

  initCache() async {

    try {
      final params = Get.arguments;
      if (params['list'] != null && params['list'] != "") {
        state.historyNumbers.value = params['list']!.split(',');
      }

      state.title.value = params['title'] ?? '未命名列表';
      state.index.value = params['index'] ?? '0';
      if (state.historyNumbers.value.isEmpty) {
        throw Exception('列表数据不能为空');
      }
    } catch (e) {
      print(Get.arguments);
      print('初始化参数失败: $e');
      // 可以添加错误处理逻辑，比如显示错误提示
      Get.snackbar('错误', '初始化列表数据失败');
    }
  }

  void SaveList() {
    print(state.historyNumbers.value);
    print(state.index.value);
    // 处理置顶逻辑
    final  RandListListLogic1 = Get.put(RandListLogic());
    // 检查参数是否为空
    final index = state.index.value; // 确保 index 是字符串类型
    final list = state.historyNumbers.value.join(',');

    if (index != null && list != null) {
      RandListListLogic1.addCustomList(index, list: list);
    } else {
      print('index 或 list 为空，无法调用 addCustomList 方法');
      Get.snackbar('错误', '保存列表数据失败，参数为空');
    }

    // 执行页面 变量更新

    /*  final RandComState stateList = Get.find<RandComLogic>().state;
       stateList.randComArgs.value.list= state.historyNumbers.value.join(',');*/
  }

// ... 其他代码保持不变 ...


}
